/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETUndoHead.h
  \date			september 2011
  \author		TNick

  \brief		Contains the definition for ETUndoHead class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __ETUNDOHEAD_INC__
#define __ETUNDOHEAD_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<main.h>

class		ETI_CplxTxt;
class		ETSheetView;
class		ETSheet;
class		ETUndoElem;


#include	<generic/cc_l2d.h>



/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/// class representing the base of an undo-redo system in a sheet
/**
*	crt_ptr element holds a pointer to the current element in the chain; this
*	is the first item to be redone and may be NULL, meaning that there is no
*	element to redone and first to be undone is first in chain; to get first
*	element to be undone if crt_ptr is != NULL use ETUndoElem::next()
*
*	If an item is added using addUndoItem() and crt_ptr is non-null, all items
*	preceding crt_ptr item and the item itself are deleted. New item is
*	always placed as the first item in chain.
*
*	External factors may tigger an undo using performUndo(); performRedo()
*	is it's counterpart. The functions may also be provided with an argument
*	telling how manny actions to perform.
*/
class ETUndoHead	: public CC_HDL2D	{

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */

	//	/// one bit switches
	//	enum Flags		{
	//		NOFLAGS = 0,

	//		MAX_FLAGS
	//	};


	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

private:

	/// associated sheet
	ETSheet *		asc_sheet;

	/// maximum number of entries to keep; use -1 for infinite
	int				undo_bf_size;

	/// number of actions that may be redone at this time
	int				redo_count;

	/// current pointer
	ETUndoElem *	crt_ptr;

protected:


	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */

public:

	/// constructor
	/**
   */
	ETUndoHead			(ETSheet * sht);


	/// destructor
	/**
  */
	virtual				~ETUndoHead		( void );


	/// retrieve the associated sheet
	ETSheet *			assocSheet		( void )
	{ return asc_sheet; }



	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Reimplementing some CC_HDL2D functionality    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// get first item
	inline ETUndoElem*	first			( void )  const
	{ return (ETUndoElem*)CC_HDL2D::first(); }


	/// get last item
	ETUndoElem*			last			( void )  const
	{ return (ETUndoElem*)CC_HDL2D::last(); }


	/// get nth item
	ETUndoElem*			item			(int i) const
	{ return (ETUndoElem*)CC_HDL2D::item(i); }


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Maximum number of entries in list    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// change the maximum number of entries that may be stored
	void				setMaxCount		(int new_max);


	/// tell the maximum number of entries that may be stored
	inline int			maxCount		(void)
	{ return undo_bf_size; }


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Main functions    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// add a new undo item to this chain
	void				addUndoItem		(ETUndoElem* new_it);


	/// perform the undo action
	void				performUndo		( int cnt = 1 );


	/// perform the redo action
	void				performRedo		( int cnt = 1 );


	/// return the number of Undo actions available from current point
	inline int			countUndo		( void )
	{ return count() - redo_count; }


	/// return the number of Redo actions available from current point
	inline int			countRedo		( void )
	{ return redo_count; }


	/// get the informative text for an action
	/**
	*	@return either the text or an empty string if the index was out of bounds
	*/
	QString				infoText		(
		bool		in_undo_chain,	/**< if set to true, the item is
	 determined by searching the undo chain; if set to false, the redo
	 chain is searched */
		int			index			/**< index of the item for which
	 the caller needs informative text; the interpretation depends
	 uppon \b in_undo_chain parameter and is relative to current item */
		);


	/// get the informative text for an action
	/**
	*	@note this is an overload
	*	@return either the text or an empty string if the index was out of bounds
	*/
	QString				infoText		(
		int			index			/**< index of the item for which
	the caller needs informative text; the index is relative to the chain
	and does not take current item into consideration */
		);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class ETUndoHead	*/

/*  CLASS    =============================================================== */
//
//
//
//
#endif // __ETUNDOHEAD_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
